package edu.tms.dao;

import java.util.List;

import edu.tms.bean.Audit;
import edu.tms.crud.JdbcUtil;
import edu.tms.dao.common.CommnonDao;


@SuppressWarnings("rawtypes")
public class AuditDaoImp implements CommnonDao{

	@Override
	public long save(Object value) throws Exception {
		if (value instanceof Audit) {
			Audit rev=(Audit)value;
			String sql="insert into t_audit values(null,?,?,?,?,?)";
			return JdbcUtil.insertAuto(sql,rev.getAuditTitle(),rev.getAuditContent(),rev.getAuditSubmitter(),rev.getCreateTime(),
				rev.getAuditState());
		}
		return 0;
	}

	@Override
	public int update(Object value) throws Exception {
		if (value instanceof Audit) {
			Audit rev=(Audit)value;
			String sql="update t_audit set audit_title=?,audit_content=?,audit_submitter=?,create_time=?,audit_state=? where audit_id=?";
			return JdbcUtil.update(sql,rev.getAuditTitle(),rev.getAuditContent(),rev.getAuditSubmitter(),rev.getCreateTime(),
					rev.getAuditState(),rev.getAuditId());
		}
		return 0;
	}

	@Override
	public int delete(Object value) throws Exception {
		if (value instanceof Audit) {
			Audit rev=(Audit)value;
			String sql="delete t_audit,t_audit_log from t_audit "
					+ "left join t_audit_log on t_audit.audit_id=t_audit_log.audit_id "
					+ "where audit_id = ? ";
			return JdbcUtil.update(sql,rev.getAuditId());
		}
		return 0;
	}

	@Override
	public List findAll(int pageNo, int pageSize) throws Exception {
		String sql = "select * from t_audit,t_character_role,t_user where t_audit.audit_submitter = t_character_role.char_id and t_user.user_id=t_audit.audit_submitter limit ?,?";
		return JdbcUtil.query(Audit.class,sql,(pageNo-1)*pageSize,pageSize);
	}

	@Override
	public List findAll(Object key, int pageNo, int pageSize) throws Exception {
		String sql="select * from t_audit where audit_id=? or audit_title=? or audit_submitter=? limit ?,?";
		return JdbcUtil.query(Audit.class,sql,key,key,key,(pageNo-1)*pageSize,pageSize);
	}

	@Override
	public Object findByKey(Object key) throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List getMapList() throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

}
